package ynu.edu.Service.Impl;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import ynu.edu.Dao.DeviceDao;
import ynu.edu.Dao.TemperatureDao;
import ynu.edu.Entity.Carbon;
import ynu.edu.Entity.Device;
import ynu.edu.Entity.Temperature;
import ynu.edu.Service.ISensorService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TemperatureService implements ISensorService {
    @Resource
    DeviceDao deviceDao;
    @Resource
    TemperatureDao temperatureDao;
    @Override
    public void insert(Double value){
        Temperature temperature = new Temperature();
        Device device = deviceDao.getReferenceById(2L);
        temperature.setDevice(device);
        temperature.setValue(value);

        temperatureDao.save(temperature);
    }
    @Override
    public Double getValue(){
        Temperature temperature = temperatureDao.findFirstByOrderByCreateTimeDesc().get();
        return temperature.getValue();
    }
    @Override
    public List<Double> getValuesFromLastDay() {
        LocalDateTime oneDayAgo = LocalDateTime.now().minusDays(1);
        List<Double> values = new ArrayList<>();
        List<Temperature> temperatures = temperatureDao.findByCreateTimeAfter(oneDayAgo);
        for (Temperature temperature : temperatures){
            values.add(temperature.getValue());
        }
        return values;
    }
    @Override
    public List<Date> getTimeFromLastDay(){
        return null;
    }
}
